﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Acms.Bll;
using Acms.AcmsControls;

public partial class Templates_DefaultMaster : System.Web.UI.MasterPage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        lvFooter.DataBinding += new EventHandler(lvFooter_DataBinding);
        if (!IsPostBack &&  BaseTemplatePage != null && BaseTemplatePage.AcmsContext != null)
        {
            dsFooterMenu.SiteId = BaseTemplatePage.AcmsContext.SiteId;
            dsFooterMenu.CurrentSiteVersion = BaseTemplatePage.AcmsContext.SiteVersion;
        }
    }

    void lvFooter_DataBinding(object sender, EventArgs e)
    {
        
    }

    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindMenu();
        }
    }

    public BaseTemplatePage BaseTemplatePage
    {
        get 
        {
            return Page as BaseTemplatePage;
        }
    }

    public override void DataBind()
    {
        base.DataBind();
    }

    private void BindMenu()
    {
        mainMenu.Items.Clear();
        TemplatePage page = Page as TemplatePage;
        if (page != null)
        {
            Acms.Bll.Menu menu = Acms.Bll.Menu.GetByNameAndSiteId("MainMenu", page.AcmsContext.SiteId);
            if (menu != null)
            {
                //find root
                Post homepage = Post.GetTopLevelBySiteId(page.AcmsContext.SiteId, page.AcmsContext.SiteVersion);
                mainMenu.Items.Add(CreateItem(homepage, null));
                mainMenu.Items[0].Expanded = true;
                if (homepage.Id == page.AcmsContext.PostId)
                {
                    mainMenu.Items[0].Selected = true;
                }
                BindToMenu(Post.GetAllByMenuIdAndParentGuid(menu.Id, homepage.PostGuid, page.AcmsContext.SiteVersion), mainMenu.Items[0], menu.Id, page.AcmsContext);
                if (Request.IsAuthenticated)
                {
                    RadPanelItem logoutItem = new RadPanelItem();
                    logoutItem.Text = "Logout";
                    logoutItem.NavigateUrl = Page.ResolveUrl(string.Format("~/Logout.aspx?ReturnUrl={0}", Server.UrlEncode(page.AcmsContext.OriginalUrl)));
                    mainMenu.Items[0].Items.Add(logoutItem);
                }
            }
        }
    }

    private void BindToMenu(List<Post> posts, RadPanelItem item, int menuId, AcmsContext context)
    {
        foreach (Post post in posts)
        {
            if(Request.IsAuthenticated && post.Path.ToLower() == "~/login")
            {
                continue;
            }
            string qs = null;
            if (post.Path.ToLower() == "~/login")
            {
                qs = string.Concat("ReturnUrl=", Server.UrlEncode(context.OriginalUrl));
            }
            RadPanelItem child = CreateItem(post, qs);
            bool expanded = false;
            if (post.Id == context.PostId)
            {
                child.Selected = true;
                child.Expanded = true;
                expanded = true;
            }
            else if(context.CurrentPost != null && context.CurrentPost.Path.StartsWith(post.Path))
            {
                child.Expanded = true;
                expanded = true;
            }
            item.Items.Add(child);

            //dont bind private if not autenticated
            List<Post> tmpChildren = Post.GetAllByMenuIdAndParentGuid(menuId, post.PostGuid, context.SiteVersion);
            if (!Request.IsAuthenticated && (post.Path.ToLower() == "~/private"))
            {
                if (tmpChildren.Count > 0)
                {
                    item.Items[item.Items.Count - 1].CssClass = "expandable";
                }
            }
            else if (expanded)
            {
                if (tmpChildren != null && tmpChildren.Count > 0)
                {
                    item.Items[item.Items.Count - 1].CssClass = "expanded";
                    BindToMenu(tmpChildren, child, menuId, context);
                }
                else
                {
                    item.Items[item.Items.Count - 1].Selected = true;
                }
            }
            else
            {
                if (tmpChildren.Count > 0)
                {
                    item.Items[item.Items.Count - 1].CssClass = "expandable";
                }
            }
        }
    }

    private RadPanelItem CreateItem(Post post, string queryString)
    {
        RadPanelItem item = new RadPanelItem(post.DisplayName);
        if (!string.IsNullOrEmpty(queryString))
        {
            string spacer = post.Url.Contains("?") ? "&" : "?";
            item.NavigateUrl = string.Concat(post.Url, spacer, queryString);
        }
        else
        {
            item.NavigateUrl = post.Url;
        }
        return item;
    }
}
